package com.example.it.looker.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.it.looker.R;
import com.example.it.looker.model.entity.News;
import com.example.it.looker.view.CommonAdapter;

import java.util.List;

/**
 * Created by 鲁迅认识的那只猹 on 9/1/2017 3:59 PM.
 * Emial 1258730808@qq.com
 * Desc:
 */

/**
 * 新闻列表的Adapter
 */
public class NewsListAdapter extends CommonAdapter<News> {


    public NewsListAdapter(Context context, List<News> dataSource) {
        super(context, dataSource);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        News news = this.getDataSource().get(position);

        View currentView = convertView;
        MyNewsViewHolder holder;

        if (convertView == null) {
            currentView = View.inflate(getContext(), R.layout.item_news, null);
            holder = new MyNewsViewHolder(currentView);
            currentView.setTag(holder);
        }

        holder = (MyNewsViewHolder) currentView.getTag();
        Glide.with(getContext()).load(news.getImgUrl()).into(holder.ivAdvertising);
        holder.tvTitle.setText(news.getTitle());
        holder.tvPublishTime.setText(news.getPublishDate());

        return currentView;
    }


    class MyNewsViewHolder extends RecyclerView.ViewHolder {

        private TextView tvPublishTime;
        private TextView tvTitle;
        private ImageView ivAdvertising;

        public MyNewsViewHolder(View itemView) {
            super(itemView);
            ivAdvertising = (ImageView) itemView.findViewById(R.id.ivNews);
            tvTitle = (TextView) itemView.findViewById(R.id.tvNewsTitle);
            tvPublishTime = (TextView) itemView.findViewById(R.id.tvNewsPublishTime);
        }
    }
}
